package com.timguan.render.dataprocessor.processor;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by guankaiqiang on 2015/5/25.
 * processor系统参数
 */
public class CommonParametersWrapper {
    private static final String SPM_SPLITOR = ".";
    private static final String FIFTH_VID = "";
    private String spmPrefix;
    private int appId = Constants.B2C_PC_APPID;

    public CommonParametersWrapper() {
    }

    public CommonParametersWrapper(int appId, int pageId, int bindingId) {
        this.appId = appId;
        this.spmPrefix = buildSPMPrefix(appId, pageId, bindingId);
    }

    public String appendSpmPrefix(String link, int id) {
        if (StringUtils.isNotBlank(link)) {
            int indexOfHash = link.indexOf("#");
            if (link.endsWith("?")) {
                return link + "_spm=" + spmPrefix + id ;
            } else if (link.contains("?")) {
                if (indexOfHash == -1) {
                    return link + "&_spm=" + spmPrefix + id ;
                } else {
                    return link.substring(0, indexOfHash) + "&_spm=" + spmPrefix + id  + link.substring(indexOfHash);
                }
            } else {
                if (indexOfHash == -1) {
                    return link + "?_spm=" + spmPrefix + id;
                } else {
                    return link.substring(0, indexOfHash) + "?_spm=" + spmPrefix + id  + link.substring(indexOfHash);
                }
            }
        } else if (link == null) {
            return null;
        } else {
            return "";
        }
    }

    public String appendSpmPrefix(String link, int id, String suffix){
        String linkSpm = appendSpmPrefix(link, id);
        if(StringUtils.isNotBlank(linkSpm)){
            linkSpm += "-" + suffix;
        }
        return linkSpm;
    }
    public String getVid(String link, int id){
        String spmPrefixDeal = null;
        if(spmPrefix.indexOf(".") != -1){
            spmPrefixDeal = spmPrefix.substring(spmPrefix.indexOf(".") + 1, spmPrefix.length());
        }
        if(StringUtils.isBlank(spmPrefixDeal)){
            return null;
        }
        if (StringUtils.isNotBlank(link)) {
            String itemId = getItemId(link);
            return spmPrefixDeal + id + SPM_SPLITOR + itemId;
        }
        else{

            return spmPrefixDeal + id + SPM_SPLITOR + FIFTH_VID;
        }

    }
    private static final Pattern ITEMID_PATTERN = Pattern.compile("([\\d]+)(?=\\.html)");

    private String getItemId(String link){
        if(link != null){
            if(link.contains(".com/detail/")){
                Matcher matcher = ITEMID_PATTERN.matcher(link);
                if(matcher.find())    {
                    return matcher.group();
                }
            }
        }

        return FIFTH_VID;
    }

    /**
     * 商品
     *
     * @param appid
     * @param pageId
     * @param bindingId
     * @return
     */
    private String buildSPMPrefix(int appid, int pageId, int bindingId) {
        StringBuilder sb = new StringBuilder();
        if (Constants.B2C_PC_APPID == appid) {
            sb.append(Constants.B2C_PC_SPM_VALUE).append(SPM_SPLITOR).append(pageId).append(SPM_SPLITOR).append(
                    bindingId).append(SPM_SPLITOR);
        } else if (Constants.B2C_IOS_APPID == appid) {
            sb.append(Constants.B2C_IOS_SPM_VALUE).append(SPM_SPLITOR).append(pageId).append(SPM_SPLITOR).append(
                    bindingId).append(SPM_SPLITOR);
        } else if (Constants.B2C_ANDROID_APPID == appid) {
            sb.append(Constants.B2C_ANDROID_SPM_VALUE).append(SPM_SPLITOR).append(pageId).append(SPM_SPLITOR).append(
                    bindingId).append(SPM_SPLITOR);
        } else if (Constants.B2C_H5_APPID == appid) {
            sb.append(Constants.B2C_H5_SPM_VALUE).append(SPM_SPLITOR).append(pageId).append(SPM_SPLITOR).append(
                    bindingId).append(SPM_SPLITOR);
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return "CommonParametersWrapper{" +
                "spmPrefix='" + spmPrefix + '\'' +
                ", appId=" + appId +
                '}';
    }
}
